package com.schoolET.controller.wechat;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.vo.AddGoodsVO;
import com.schoolET.error.costom.NoGoodsTypeException;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.wechat.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author: lin
 * @className: GoodsController
 * @createTime: 2024-06-06  10:40
 * @description: 商品控制器
 */
@Api(tags = "用户商品")
@RestController
@RequestMapping("/schoolET/wx")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "商品查询")
    @PostMapping("/queryGoods/{id}")
    @SaCheckLogin
    public Result queryGoods(@PathVariable Integer id) {
        return Result.success(goodsService.getGoodsById(id)).msg("查询成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "商品图片")
    @PostMapping("/queryGoods/img")
    @SaCheckLogin
    public Result queryGoods(MultipartFile file) throws IOException {
        return Result.success(goodsService.putImage(file)).msg("添加成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "发布商品")
    @PostMapping("/queryGoods/release")
    @SaCheckLogin
    public Result release(@Valid @RequestBody AddGoodsVO addGoodsVO) {
        if (goodsService.getGoodsTypeById(addGoodsVO.getTypeId()) == null) {
            throw new NoGoodsTypeException();
        }
        String loginIdAsString = StpUtil.getLoginIdAsString();
        goodsService.addGoods(addGoodsVO, loginIdAsString);
        return Result.success().msg("商品添加成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "我购买的商品")
    @PostMapping("/myBuy")
    @SaCheckLogin
    public Result myBuy(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "5")Integer pageSize) {
        pageNum = pageNum < 0 ? 1 : pageNum;
        pageSize = pageSize < 0 ? 5 : pageSize;
        String loginIdAsString = StpUtil.getLoginIdAsString();
        return Result.success(MyPage.page(goodsService.getMyPurchaseGoods(loginIdAsString,pageNum, pageSize))).msg("查询成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "我发布的商品")
    @PostMapping("/mySell")
    @SaCheckLogin
    public Result mySell(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "5")Integer pageSize) {
        pageNum = pageNum < 0 ? 1 : pageNum;
        pageSize = pageSize < 0 ? 5 : pageSize;
        String loginIdAsString = StpUtil.getLoginIdAsString();
        return Result.success(goodsService.getMySellGoods(loginIdAsString,pageNum, pageSize)).msg("查询成功");
    }


    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "下架商品")
    @PostMapping("/orderReset/{id}")
    @SaCheckLogin
    public Result orderReset(@PathVariable Integer id) {
        String loginIdAsString = StpUtil.getLoginIdAsString();
        goodsService.takenShelves(id, loginIdAsString);
        return Result.success().msg("修改成功");
    }


    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "查找商品")
    @PostMapping("/invitation")
    @SaCheckLogin
    public Result invitation(@RequestParam(required = false)Integer goodsTypeId,
                             @RequestParam(required = false) String theme,
                             @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "5")Integer pageSize) {
        pageNum = pageNum < 0 ? 1 : pageNum;
        pageSize = pageSize < 0 ? 5 : pageSize;
        String loginIdAsString = StpUtil.getLoginIdAsString();
        return Result.success(goodsService.getAllGoodsByTypeAndTheme(loginIdAsString, goodsTypeId, theme, pageNum, pageSize)).msg("查找成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "查找类型")
    @PostMapping("/getType")
    @SaCheckLogin
    public Result getType() {
        return Result.success(goodsService.getGoodsTypeInfo()).msg("查找成功");
    }

}
